package master.photo.controller

import org.springframework.beans.factory.annotation.Autowired;

import master.photo.domain.Profile;
import master.photo.domain.security.AppUser;
import master.photo.domain.security.UserRole 
import master.photo.service.GroovyImageResizer;

class ProfileController {
	
	def scaffold = Profile;
	
	@Autowired
	private GroovyImageResizer imageResizer;
	
	def save = {
		def profile = new Profile()
		profile.properties = params
		if (profile.photo) {
			profile.photo = imageResizer.scaleJPG(profile.photo, 200, 200);
		}
		if (profile.save()) {
			def appUser = AppUser.get(session['USER_LOGGED_IN'].id);
			appUser.profile = profile
			appUser.save()
			redirect(action: 'show', params:['id':profile.id])
		} else {
			render view: 'create', model: [authorityList: UserRole.list()]
		}
	}
	
}
